package com.ykdl.member.kid.doctor;

import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.AskDoctorAdapter;
import com.ykdl.member.kid.beans.AskDoctorBean;
import com.ykdl.member.kid.beans.AskDoctorBeanListBean;
import com.ykdl.member.kid.beans.DoctorOnlineBean;
import com.ykdl.member.kid.beans.DoctorsBean;
import com.ykdl.member.kid.doctor.DoctorOnlineActivity.getDoctorQuestionsListTag;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class ActionsDoctorOnlineActivity extends BaseScreen implements OnClickListener{
	private AsyncImageView doctor_head ;
	private TextView doctor_name ;
	private TextView doctor_position ;
	private TextView doctor_subject ;
	private TextView doctor_onlinetime ;
	private AskDoctorAdapter adapter ;
	private RefreshListView ask_doctor_list ;
	private String TAG = "ActionsDoctorOnlineActivity";
	private ImageButton answer_question,allanswer_question ,ask_thedoctor;
	private DoctorOnlineBean bean ;
	private String campaign_id;
	private boolean isshow_bt ;
	private boolean isfirst =true ;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 40;
	private boolean is_ansewer =true ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("专家坐诊", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.actionsdoctoronlineactivity_xml);
		campaign_id = this.getIntent().getStringExtra(KidAction.DOCTOR_CAMPAIGN_ID);
		isshow_bt = this.getIntent().getBooleanExtra(KidAction.IS_GONE_BT, true) ;
		inint();
		inintdata(is_ansewer);
	}
	public void inint() {
		ask_thedoctor = (ImageButton)findViewById(R.id.ask_thedoctor);
		if (!isshow_bt) {
			ask_thedoctor.setVisibility(View.GONE);
		}
		answer_question = (ImageButton)findViewById(R.id.answer_question);
		allanswer_question = (ImageButton)findViewById(R.id.allanswer_question);
		findViewById(R.id.go_action_bt).setOnClickListener(this);
		answer_question.setSelected(true);
		answer_question.setOnClickListener(this);
		allanswer_question.setOnClickListener(this);
		ask_thedoctor.setOnClickListener(this);
		doctor_head = (AsyncImageView)findViewById(R.id.doctor_head);
		doctor_head.setOnClickListener(this);
		doctor_name = (TextView)findViewById(R.id.doctor_name);
		doctor_position = (TextView)findViewById(R.id.doctor_position);
		doctor_subject = (TextView)findViewById(R.id.doctor_subject);
		doctor_onlinetime = (TextView)findViewById(R.id.doctor_onlinetime);
		ask_doctor_list = (RefreshListView)findViewById(R.id.ask_doctor_list);
		ask_doctor_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				AskDoctorBean bean= (AskDoctorBean)adapter.getItem(arg2);
				Intent intent = new Intent(ActionsDoctorOnlineActivity.this,ProblemDetailsAcivity.class);
				intent.putExtra(KidAction.DOCTOR_TOPICI_DD, bean.getTopic_id()+"");
				ActionsDoctorOnlineActivity.this.startActivity(intent);
			}
			
		});
		ask_doctor_list.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				cursor = 0;
				inintdata(is_ansewer);
			}
			});
		ask_doctor_list.setMoreClick(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				inintdata(is_ansewer);
			}});
		adapter = new AskDoctorAdapter(this,true);
	}
	public void inintdata(boolean isanswer) {
		showProgress("正在发送请稍等...");
		String  pageurl =KidConfig.GET_ONLINE_DOCTORQUESTION_LIST+campaign_id+"/questions";
		Map<String,String> map = new HashMap<String, String>();
		if (isanswer) {
			map.put("answer_status", "2,3");
		}
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,"");
		 TaskManager.startHttpRequest(request, new getDoctorQuestionsListTag(isanswer),  AskDoctorBeanListBean.class);
	}
	class getDoctorQuestionsListTag implements ITag {
		private boolean isanswer;
		public getDoctorQuestionsListTag(boolean isanswer) {
			this.isanswer = isanswer ;
		}
		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof AskDoctorBeanListBean) {
				AskDoctorBeanListBean bean = (AskDoctorBeanListBean)t;
				adapter.setList(bean.getList());
				ask_doctor_list.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				if (isfirst) {
					setHeadData(bean.getProfession_campaign());
				}
				isfirst = false;
				if (bean.getNext_cursor() == bean.getTotal_number()) {
					ask_doctor_list.setMoreButtoIsGon(true);
				} else {
					ask_doctor_list.setMoreButtoIsGon(false);
					cursor = bean.getNext_cursor();
				}
				ask_doctor_list.onRefreshComplete();
				ask_doctor_list.setFecthMoreOk();
				}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void setHeadData(DoctorOnlineBean bean) {
		if (bean.getDoctor()!=null) {
			if (bean.getDoctor().getAvatar_file_meta()!=null) {
				String url = bean.getDoctor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				doctor_head.setUrl(TAG, url, ImageOptionsFactory.getSmallPortraitOptions(ActionsDoctorOnlineActivity.this));
			}
			doctor_head.setTag(bean.getDoctor().getActor_id());
			doctor_name.setText(bean.getDoctor().getDisplay_name());
			if (bean.getDoctor().getProfile()!=null) {
				doctor_position.setText(bean.getDoctor().getProfile().getTitle());
			}
		}
			doctor_subject.setText("主题:"+bean.getTitle());
			doctor_onlinetime.setText("时间:"+DateUtil.getDateDatatimeSting(bean.getStart_time())+"-"+DateUtil.getDateDatatimeSting(bean.getEnd_time()));
	}
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.answer_question:
			answer_question.setSelected(true);
			allanswer_question.setSelected(false);
			is_ansewer=true;
			inintdata(is_ansewer);
			break;
		case R.id.allanswer_question:
			answer_question.setSelected(false);
			allanswer_question.setSelected(true);
			is_ansewer=false;
			inintdata(is_ansewer);
			break;
		case R.id.ask_thedoctor:
			Intent intent = new Intent(ActionsDoctorOnlineActivity.this,
					ToAskDoctorActivity.class);
			intent.putExtra("campaign_id", campaign_id);
			ActionsDoctorOnlineActivity.this.startActivity(intent);
			break;
		case R.id.doctor_head:
			Intent intent1 = new Intent(mContext, DoctorOnlineActivity.class);
			intent1.putExtra("doctor_id", doctor_head.getTag()+"");
			intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(intent1);
			break;
		case R.id.go_action_bt:
			Intent intent2 = new Intent(ActionsDoctorOnlineActivity.this,CampaignInfoWebView.class);
			intent2.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, campaign_id+"");
			ActionsDoctorOnlineActivity.this.startActivity(intent2);
			break;
		default:
			break;
		}
	}
	

}
